package business

import (
	"errors"
	"ginBasic/app/common/model/filter"
	"github.com/gin-gonic/gin"
	"strings"

	"ginBasic/app"
	"ginBasic/app/common/lib"
	"ginBasic/app/common/model"
)

var Role = new(RoleBus)

type RoleBus struct {
	BaseBus
}

func (b RoleBus) RoleList() []interface{} {
	roleList := model.GetRoleList()
	List := make([]interface{}, 0)
	for _, v := range roleList {
		List = append(List, map[string]interface{}{
			"id":      v.Id,
			"title":   v.Title,
			"status":  v.Status,
			"addTime": lib.UnixToTime(v.AddTime),
		})
	}
	return List
}

func (b RoleBus) DoAddRole(c *gin.Context) (flag bool, msg string) {
	if c.Request.Method != "POST" {
		return false, "请求方式有误"
	}
	var req *model.Role
	// 获取参数并验证
	if err := c.ShouldBind(&req); err != nil {
		return false, "请求方式有误"
	}
	if req.Title == "" {
		return false, "角色名称不能为空"
	}
	res := model.AddRole(req.Title, req.Description, req.Status)
	if !res {
		return true, "添加失败"
	}
	return true, "添加成功"
}
func (b RoleBus) DoEditRole(c *gin.Context) (flag bool, msg string) {
	if c.Request.Method != "POST" {
		return false, "请求方式有误"
	}
	var req *model.Role
	// 获取参数并验证
	if err := c.ShouldBind(&req); err != nil {
		return false, "登录有误"
	}
	if req.Title == "" {
		return false, "角色名称不能为空"
	}
	res := model.EditRole(*req)
	if !res {
		return true, "修改失败"
	}
	return true, "修改成功"
}

func (b RoleBus) GetRoleDetail(id int) model.Role {
	var info model.Role
	info = model.GetRoleById(id)
	return info
}

func (b RoleBus) DeleteRole(ids string) (int64, error) {

	idsArr := strings.Split(ids, ",")
	//判断是否存在超级管理员
	_, found := app.Find(idsArr, "9")
	if found {
		return 0, errors.New("超级管理员不能删除")
	}
	res, err := model.DeleteRole(idsArr)
	if res {
		count := int64(len(idsArr))
		return count, nil
	} else {
		return 0, err
	}
}

func (b RoleBus) UpdateRoleStatus(c *gin.Context) (bool, string) {
	if c.Request.Method != "POST" {
		return false, "请求方式有误"
	}
	var req *model.Role
	// 获取参数并验证
	if err := c.ShouldBind(&req); err != nil {
		return false, "请求方式有误"
	}
	if req.Id == 9 {
		return false, "超级管理员不能关闭"
	}
	_, err := model.UpdateRoleStatus(*req)
	if err != nil {
		return false, err.Error()
	} else {
		return true, "操作成功"
	}
}

func (b RoleBus) GetAuthList(id int) []*filter.AccessAuth {
	roleAccessMap := b.GetAccessAndRole(id)
	where := "1=1"
	accessList := model.AccessModel.List(where)
	List := make([]*filter.AccessAuth, 0)
	for _, v := range accessList {
		checked := false
		if _, ok := roleAccessMap[v.Id]; ok {
			checked = true
		}
		List = append(List, &filter.AccessAuth{
			Id:       v.Id,
			ParentId: v.ModuleId,
			Value:    v.ModuleName,
			List:     nil,
			Checked:  checked,
			Disabled: false,
		})
	}
	resList := b.getAuthTree(List, 0)
	return resList
}

func (b RoleBus) getAuthTree(list []*filter.AccessAuth, parentId int) []*filter.AccessAuth {
	res := make([]*filter.AccessAuth, 0)
	for _, v := range list {
		if v.ParentId == parentId {
			v.List = b.getAuthTree(list, v.Id)
			res = append(res, v)
		}
	}
	return res
}

func (b RoleBus) SaveRoleAndAccess(id int, accessIds string) (bool, error) {
	roleAccess := make([]*model.RoleAccess, 0)
	accessIdsArr := strings.Split(accessIds, ",")
	for _, v := range accessIdsArr {
		accessId, _ := lib.Int(v)
		roleAccess = append(roleAccess, &model.RoleAccess{
			RoleId:   id,
			AccessId: accessId,
		})
	}
	//删除原来的数据
	_, derr := model.AccessRoleModel.Delete(id)
	if derr != nil {
		return false, derr
	}
	//保存数据
	_, rerr := model.AccessRoleModel.Add(roleAccess)
	if rerr != nil {
		return false, rerr
	}
	return true, nil
}
